package com.learn.learnredis.jedis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author wangxing
 * @version 2020/7/16 16:16 Administrator
 */
public class JedisSingle {
    private  static JedisPoolConfig jedisPoolConfig;
    private static final String REDIS_URL="192.168.1.151";
    private static final int PORT = 6379;
    private static final int TIMEOUT =2000;
    private  static final String PASSWORD = null;
    static{
        jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMinIdle(5);
        jedisPoolConfig.setMaxIdle(10);
        jedisPoolConfig.setMaxTotal(20);
        jedisPoolConfig.setMaxWaitMillis(2000);
    }
    public static JedisPool getJedisPool(){
        return JedisPoolFactory.jedisPool;
    }
    public static void set(String key,String value){
        Jedis jedis =  getJedisPool().getResource();
        jedis.set(key, value);
    }
    public static String get(String key){
        Jedis jedis =  getJedisPool().getResource();
        return jedis.get(key);
    }

    static class JedisPoolFactory{
        static JedisPool jedisPool = new JedisPool(jedisPoolConfig,REDIS_URL,PORT,TIMEOUT,PASSWORD);
    }
}
